<?php
class SearchMaterialModelAction extends CAction {
	public function run($materialId, $start = 0, $limit = 25) {
		$materialId = intval($materialId);
		$model = MaterialModel::model();
		if ($materialId) {
			$criteria = new CDbCriteria();
			$criteria->compare('materialId', $materialId);
			$criteria->order = 'model DESC';

			$count = $model->count($criteria);

			$criteria->offset = $start;
			$criteria->limit = $limit;

			$materialModels = $model->findAll($criteria);
			Yii::app()->recent->add('Material', $materialId);
			echo CJSON::encode(
				array(
					'success' => true,
					'data' => $materialModels,
					'total' => $count
				));
		} else {
			echo CJSON::encode(
				array(
					'success' => true,
					'data' => array(),
					'total' => 0
				));
		}
	}
}
